package com.aizuda.encrypt.config;

import com.aizuda.encrypt.helper.DecryptToolKit;
import com.aizuda.encrypt.helper.RemoteConfigToolKit;
import com.aizuda.encrypt.interceptor.DecryptQueryInterceptor;
import com.aizuda.encrypt.interceptor.EncryptQueryInterceptor;
import com.aizuda.encrypt.interceptor.EncryptSaveInterceptor;
import com.aizuda.encrypt.service.ReadRemoteConfigService;
import com.aizuda.encrypt.service.TransTableService;
import com.aizuda.encrypt.service.impl.ApolloReadRemoteConfigServiceImpl;
import com.aizuda.encrypt.service.impl.TransTableZenWithBuddhaImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;

import java.util.List;

/**
 * Mybatis 数据加密自动配置
 *
 * @author nn200433
 * @date 2024-03-22 05:18:29
 */
@Import(cn.hutool.extra.spring.SpringUtil.class)
@EnableConfigurationProperties(EncryptProperties.class)
public class EncryptAutoConfigure {

    /**
     * 数据 加密保存 拦截器
     *
     * @return {@link EncryptSaveInterceptor }
     * @author nn200433
     */
    @Bean
    public EncryptSaveInterceptor encryptSaveInterceptor(EncryptProperties properties) {
        return new EncryptSaveInterceptor(properties);
    }

    /**
     * 加密数据 查询 拦截器
     *
     * @return {@link EncryptQueryInterceptor }
     * @author nn200433
     */
    @Bean
    public EncryptQueryInterceptor encryptQueryInterceptor(EncryptProperties properties) {
        return new EncryptQueryInterceptor(properties);
    }

    /**
     * 加密数据 查询并解密返回 拦截器
     *
     * @return {@link DecryptQueryInterceptor }
     * @author nn200433
     */
    @Bean
    public DecryptQueryInterceptor decryptQueryInterceptor() {
        return new DecryptQueryInterceptor();
    }

    /**
     * 默认密码本转译表（与佛论禅）
     *
     * @return {@link TransTableService }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean
    public TransTableService defaultTransTable() {
        return new TransTableZenWithBuddhaImpl();
    }

    /**
     * 默认Apollo远程配置读取服务实现
     *
     * @return {@link RemoteConfigToolKit }
     * @author nn200433
     */
    @Bean
    public ReadRemoteConfigService defaultApolloReadRemoteConfigServiceImpl() {
        return new ApolloReadRemoteConfigServiceImpl();
    }

    /**
     * 远程配置工具包
     *
     * @param serviceList 服务列表
     * @return {@link RemoteConfigToolKit }
     * @author nn200433
     */
    @Bean
    public RemoteConfigToolKit remoteConfigToolKit(List<ReadRemoteConfigService> serviceList) {
        return new RemoteConfigToolKit(serviceList);
    }

    /**
     * 解密工具包（扫描数据库表信息）
     *
     * @return {@link DecryptToolKit }
     * @author nn200433
     */
    @Bean
    @DependsOn(value = "sqlSessionFactory")
    public DecryptToolKit decryptToolKit() {
        return new DecryptToolKit();
    }

}
